﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Helpers;
namespace SLS.Client
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            /*
            foreach (Assembly assembly in assemblies)
            {
                ViewResolver.ResolveViewLookups(assemblies);
                PopupResolver.ResolvePopupLookups(assemblies);
            } 
              CinchBootStrapper.Initialise(new List<Assembly> { typeof(App).Assembly });
             */
            InitializeComponent();
        }
        /// <summary>
        /// Load the workspace values
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {

            PresentationTraceSources.ResourceDictionarySource.Listeners.Add(new ConsoleTraceListener());
            PresentationTraceSources.ResourceDictionarySource.Switch.Level = SourceLevels.All;
            PresentationTraceSources.DataBindingSource.Listeners.Add(new ConsoleTraceListener());
            PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Error;
            PresentationTraceSources.DependencyPropertySource.Listeners.Add(new ConsoleTraceListener());
            PresentationTraceSources.DependencyPropertySource.Switch.Level = SourceLevels.All;
            PresentationTraceSources.DocumentsSource.Listeners.Add(new ConsoleTraceListener());
            PresentationTraceSources.DocumentsSource.Switch.Level = SourceLevels.All;
            PresentationTraceSources.MarkupSource.Listeners.Add(new ConsoleTraceListener());
            PresentationTraceSources.MarkupSource.Switch.Level = SourceLevels.All;
            PresentationTraceSources.NameScopeSource.Listeners.Add(new ConsoleTraceListener());
            PresentationTraceSources.NameScopeSource.Switch.Level = SourceLevels.All;
            base.OnStartup(e);
            TraceConfiguration();
            try
            {
                DataAccessService.Instance.Load();
                ThemeManager.Instance.Load();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        /// <summary>
        /// Save the workspace values
        /// </summary>
        /// <param name="e"></param>
        protected override void OnExit(ExitEventArgs e)
        {
            ThemeManager.Instance.Save();
            base.OnExit(e);
        }

        private void TraceConfiguration()
        {
            try
            {
                //delete the old log file
                string logPath = Path.Combine(PathHelper.ApplicationPath, "\\log");
                logPath = PathHelper.ApplicationPath + "\\log\\log.xml";
                if (File.Exists(logPath))
                    File.Delete(logPath);
                logPath = PathHelper.ApplicationPath + "\\log\\SLCLIENT.exe.log";
                if (File.Exists(logPath))
                    File.Delete(logPath);

                if (SLS.Client.Properties.Settings.Default.UseTraceListener)
                {
                    //configure the trace
                    System.Diagnostics.Trace.AutoFlush = true;
                    System.Diagnostics.Trace.IndentSize = 2;

                    //configure the text listenner
                    System.Diagnostics.TraceListenerCollection listeners = System.Diagnostics.Trace.Listeners;
                    listeners.Add(new System.Diagnostics.TextWriterTraceListener(logPath, "LOG"));
                }
                log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                DataAccessService.Instance.m_log = m_log;
                /*
                  WorkspaceService.Instance.TracerLevel = SLS.ExClassLib.Events.MessageEventType.OFF;
                  if (m_log.IsDebugEnabled)
                  {
                      WorkspaceService.Instance.TracerLevel = SLS.ExClassLib.Events.MessageEventType.DEBUG;
                  }
                  if (m_log.IsErrorEnabled)
                  {
                      WorkspaceService.Instance.TracerLevel = SLS.ExClassLib.Events.MessageEventType.ERROR;
                  }
                  if (m_log.IsFatalEnabled)
                  {
                      WorkspaceService.Instance.TracerLevel = SLS.ExClassLib.Events.MessageEventType.FATAL;
                  }
                  if (m_log.IsInfoEnabled)
                  {
                      WorkspaceService.Instance.TracerLevel = SLS.ExClassLib.Events.MessageEventType.INFO;
                  }
                  if (m_log.IsWarnEnabled)
                  {
                      WorkspaceService.Instance.TracerLevel = SLS.ExClassLib.Events.MessageEventType.WARN;
                  }*/

            }
            catch (Exception ex)
            {
                Tracer.Error("LanSunStudio.TraceConfiguration", ex);
            }
        }
    }
}
